package com.bj58.mis.apm.server.kylin.call;

import com.bj58.mis.apm.server.kylin.util.JsonUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;

public class ApmTest1Call extends BaseCall{

    public ApmTest1Call(String projectName, int thread, int loop) {
        super(projectName, thread, loop);
    }

    public static void main(String[] args) throws Exception {
        new ApmTest1Call("apm_test1", 1, 1);
    }

    public final Random random = new Random();

    @Override
    public void query(Connection conn) throws Exception{
        String str = "select PROJECT_ID,count(1) from ODS.O_M_KYLIN_QUERY_LOG s group by s.PROJECT_ID";
        PreparedStatement state = conn.prepareStatement(str);
        ResultSet resultSet = state.executeQuery();
        while(resultSet.next()){
            System.out.println(JsonUtil.toString(resultSet));
            System.out.println();
        }
    }

    /*
    @Override
    public void query(Connection conn) throws Exception{
        String str = "select $1,$2 from o_m_t_score s left join o_m_t_subject j on s.subject_id = j.id left join o_m_t_student d on s.user_id = d.id where 1=1 $5 group by $3";
        List<String> p1 = new ArrayList();
        p1.add("s.subject_id");
        p1.add("j.subject_name");
        p1.add("s.user_id");
        p1.add("d.user_name");
        p1.add("d.province");
        p1.add("d.city");
        p1.add("d.sex");

        List<String> p2 = new ArrayList();
        p2.add("sum(s.score) as sumscore");
        p2.add("max(s.score) as maxscore");
        p2.add("min(s.score) as minscore");
        p2.add("avg(s.score) as avgscore");
        p2.add("count(s.score) as countscore");

        List<String> p3 = new ArrayList();
        p3.add("subject_id");
        p3.add("subject_name");
        p3.add("user_id");
        p3.add("user_name");
        p3.add("province");
        p3.add("city");
        p3.add("sex");

        List<String> p4 = new ArrayList();
        p4.add("sumscore");
        p4.add("maxscore");
        p4.add("minscore");
        p4.add("avgscore");
        p4.add("countscore");

        List<String> p5 = new ArrayList<>();
        p5.add(" and j.subject_name = '英语'");
        p5.add(" and d.city = '唐山'");

        Set<String> result = new HashSet<>();
        Set<String> set1 = new HashSet<>();

        int lenp = random.nextInt(7)+1;
        while(lenp > 0){
            int index = random.nextInt(7);
            set1.add(p1.get(index));
            result.add(p3.get(index));
            lenp --;
        }

        Set<String> set2 = new HashSet<>();
        lenp = random.nextInt(5)+1;
        while(lenp > 0){
            int index = random.nextInt(5);
            set2.add(p2.get(index));
            result.add(p4.get(index));
            lenp --;
        }
        Set<String> set5 = new HashSet<>();
        lenp = random.nextInt(2)+1;
        while (lenp > 0){
            int index = random.nextInt(2);
            set5.add(p5.get(index));
            lenp--;
        }

        String s1 = StringUtils.join(set1.toArray(),",");
        String s2 = StringUtils.join(set2.toArray(),",");
        String s5 = StringUtils.join(set5.toArray()," ");

        str = str.replace("$1",s1);
        str = str.replace("$2",s2);
        str = str.replace("$3",s1);
        str = str.replace("$5",s5);

        System.out.println("sql:");
        System.out.println(str);

        PreparedStatement state = conn.prepareStatement(str);
        ResultSet resultSet = state.executeQuery();
        while(resultSet.next()){
            for(String key : result){
                System.out.print(resultSet.getString(key)+"\t");
            }
            System.out.println();
        }
    }
    */
}
